setwd("E:\\5hmc_file\\2_5hmc_yjp_bam\\ASM")
dir1="./bayes_pvalue_beta0/"
dir2="./bayes_BF/"
library(ggplot2)
library(scales)
library(RColorBrewer)
group1=c("X2B_X1T","M8_M7","M6_M5","M2_M1","M48_M47","M50_M49","M28_M27","M30_M29","M26_M25","M35_M36","M18_M17","M20_M19","M22_M21","M40_M39")
i=1
  fn1=paste0(dir1,group1[i],".bayes_p.txt")
  file1=read.table(fn1,head=T,sep = "\t")
  file1$unitID=paste(file1$chrom,file1$position,file1$ref,file1$var,sep=":")
  file1=data.frame(unitID=file1$unitID,normal_bayes_beta0=file1$normal_bayes_beta0,normal_bayes_pvalue=file1$normal_bayes_pvalue,tumor_bayes_beta0=file1$tumor_bayes_beta0,tumor_bayes_pvalue=file1$tumor_bayes_pvalue)
  file1$FDR1=p.adjust(file1$normal_bayes_pvalue,method = "BH")
  file1$FDR2=p.adjust(file1$tumor_bayes_pvalue,method = "BH")
  
  fn2=paste0(dir2,group1[i],".bayes_factor.txt")
  file2=read.table(fn2,head=T,sep="\t")
  file2$unitID=paste(file2$chrom,file2$position,file2$ref,file2$var,sep=":")
  file2=data.frame(unitID=file2$unitID,BayesFactor=file2$BayesFactor)
  file=merge(file2,file1,by="unitID")
  file=file[file[,7]<0.1|file[,8]<0.1,]
  file$normal_group="nosig"
  file[file$normal_bayes_beta0>0&file$FDR1<0.1,]$normal_group="up"
  file[file$normal_bayes_beta0<0&file$FDR1<0.1,]$normal_group="down"
  file$tumor_group="nosig"
  file[file$tumor_bayes_beta0>0&file$FDR2<0.1,]$tumor_group="up"
  file[file$tumor_bayes_beta0<0&file$FDR2<0.1,]$tumor_group="down"
  file$pattern.DC=paste0("normal_",file$normal_group,"-tumor_",file$tumor_group)
  file=file[,-9]
  file=file[,-9]
  names(file)=c("unitID",paste(group1[i],"BayesFactor",sep="."),
                paste0(unlist(strsplit(group1[i],"_"))[1],c(".bayes.beta0",".bayes.p.value")),
                paste0(unlist(strsplit(group1[i],"_"))[2],c(".bayes.beta0",".bayes.p.value")),
                paste0(unlist(strsplit(group1[i],"_"))[1],c(".FDR")),
                paste0(unlist(strsplit(group1[i],"_"))[2],c(".FDR")),
				paste0("pattern.DC.",group1[i]))

  rt=file
for(i in 2:6){
  fn1=paste0(dir1,group1[i],".bayes_p.txt")
  file1=read.table(fn1,head=T,sep = "\t")
  file1$unitID=paste(file1$chrom,file1$position,file1$ref,file1$var,sep=":")
  file1=data.frame(unitID=file1$unitID,normal_bayes_beta0=file1$normal_bayes_beta0,normal_bayes_pvalue=file1$normal_bayes_pvalue,tumor_bayes_beta0=file1$tumor_bayes_beta0,tumor_bayes_pvalue=file1$tumor_bayes_pvalue)
  file1$FDR1=p.adjust(file1$normal_bayes_pvalue,method = "BH")
  file1$FDR2=p.adjust(file1$tumor_bayes_pvalue,method = "BH")
  
  fn2=paste0(dir2,group1[i],".bayes_factor.txt")
  file2=read.table(fn2,head=T,sep="\t")
  file2$unitID=paste(file2$chrom,file2$position,file2$ref,file2$var,sep=":")
  file2=data.frame(unitID=file2$unitID,BayesFactor=file2$BayesFactor)
  
  file=merge(file2,file1,by="unitID")
  file=file[file[,7]<0.1|file[,8]<0.1,]
  file$normal_group="nosig"
  file[file$normal_bayes_beta0>0&file$FDR1<0.1,]$normal_group="up"
  file[file$normal_bayes_beta0<0&file$FDR1<0.1,]$normal_group="down"
  file$tumor_group="nosig"
  file[file$tumor_bayes_beta0>0&file$FDR2<0.1,]$tumor_group="up"
  file[file$tumor_bayes_beta0<0&file$FDR2<0.1,]$tumor_group="down"
  file$pattern.DC=paste0("normal_",file$normal_group,"-tumor_",file$tumor_group)
  file=file[,-9]
  file=file[,-9]
  names(file)=c("unitID",paste(group1[i],"BayesFactor",sep="."),
                paste0(unlist(strsplit(group1[i],"_"))[1],c(".bayes.beta0",".bayes.p.value")),
                paste0(unlist(strsplit(group1[i],"_"))[2],c(".bayes.beta0",".bayes.p.value")),
                paste0(unlist(strsplit(group1[i],"_"))[1],c(".FDR")),
                paste0(unlist(strsplit(group1[i],"_"))[2],c(".FDR")),
				paste0("pattern.DC.",group1[i]))
  rt=merge(rt,file,by="unitID",all=T)
}
#合并双发的
for(i in 7:10){
  fn1=paste0(dir1,group1[i],".bayes_p.txt")
  file1=read.table(fn1,head=T,sep = "\t")
  file1$unitID=paste(file1$chrom,file1$position,file1$ref,file1$var,sep=":")
  file1=data.frame(unitID=file1$unitID,normal_bayes_beta0=file1$normal_bayes_beta0,normal_bayes_pvalue=file1$normal_bayes_pvalue,tumor_bayes_beta0=file1$tumor_bayes_beta0,tumor_bayes_pvalue=file1$tumor_bayes_pvalue)
  file1$FDR1=p.adjust(file1$normal_bayes_pvalue,method = "BH")
  file1$FDR2=p.adjust(file1$tumor_bayes_pvalue,method = "BH")
  
  fn2=paste0(dir2,group1[i],".bayes_factor.txt")
  file2=read.table(fn2,head=T,sep="\t")
  file2$unitID=paste(file2$chrom,file2$position,file2$ref,file2$var,sep=":")
  file2=data.frame(unitID=file2$unitID,BayesFactor=file2$BayesFactor)
  
  file=merge(file2,file1,by="unitID")
  file=file[file[,7]<0.1|file[,8]<0.1,]
  file$normal_group="nosig"
  file[file$normal_bayes_beta0>0&file$FDR1<0.1,]$normal_group="up"
  file[file$normal_bayes_beta0<0&file$FDR1<0.1,]$normal_group="down"
  file$tumor_group="nosig"
  file[file$tumor_bayes_beta0>0&file$FDR2<0.1,]$tumor_group="up"
  file[file$tumor_bayes_beta0<0&file$FDR2<0.1,]$tumor_group="down"
  file$pattern=paste0("normal_",file$normal_group,"-tumor_",file$tumor_group)
  file$pattern.CC="other"
  file[file$pattern=="normal_down-tumor_down",]$pattern.CC="normal_down_tumor_down"
file[file$pattern=="normal_down-tumor_nosig"|file$pattern=="normal_nosig-tumor_down",]$pattern.CC="one_is_down"
file[file$pattern=="normal_down-tumor_up"|file$pattern=="normal_up-tumor_down",]$pattern.CC="onedown_oneup"
file[file$pattern=="normal_nosig-tumor_up"|file$pattern=="normal_up-tumor_nosig",]$pattern.CC="one_is_up"
file[file$pattern=="normal_up-tumor_up",]$pattern.CC="normal_up_tumor_up"
  file=file[,-9]
  file=file[,-9]
  file=file[,-9]
  names(file)=c("unitID",paste(group1[i],"BayesFactor",sep="."),
                paste0(unlist(strsplit(group1[i],"_"))[1],c(".bayes.beta0",".bayes.p.value")),
                paste0(unlist(strsplit(group1[i],"_"))[2],c(".bayes.beta0",".bayes.p.value")),
                paste0(unlist(strsplit(group1[i],"_"))[1],c(".FDR")),
                paste0(unlist(strsplit(group1[i],"_"))[2],c(".FDR")),
				paste0("pattern.CC.",group1[i]))
  rt=merge(rt,file,by="unitID",all=T)
}
#合并健康的
for(i in 11:14){
  fn1=paste0(dir1,group1[i],".bayes_p.txt")
  file1=read.table(fn1,head=T,sep = "\t")
  file1$unitID=paste(file1$chrom,file1$position,file1$ref,file1$var,sep=":")
  file1=data.frame(unitID=file1$unitID,normal_bayes_beta0=file1$normal_bayes_beta0,normal_bayes_pvalue=file1$normal_bayes_pvalue,tumor_bayes_beta0=file1$tumor_bayes_beta0,tumor_bayes_pvalue=file1$tumor_bayes_pvalue)
  file1$FDR1=p.adjust(file1$normal_bayes_pvalue,method = "BH")
  file1$FDR2=p.adjust(file1$tumor_bayes_pvalue,method = "BH")
  
  fn2=paste0(dir2,group1[i],".bayes_factor.txt")
  file2=read.table(fn2,head=T,sep="\t")
  file2$unitID=paste(file2$chrom,file2$position,file2$ref,file2$var,sep=":")
  file2=data.frame(unitID=file2$unitID,BayesFactor=file2$BayesFactor)
  
  file=merge(file2,file1,by="unitID")
  file=file[file[,7]<0.1|file[,8]<0.1,]
  file$normal_group="nosig"
  file[file$normal_bayes_beta0>0&file$FDR1<0.1,]$normal_group="up"
  file[file$normal_bayes_beta0<0&file$FDR1<0.1,]$normal_group="down"
  file$tumor_group="nosig"
  file[file$tumor_bayes_beta0>0&file$FDR2<0.1,]$tumor_group="up"
  file[file$tumor_bayes_beta0<0&file$FDR2<0.1,]$tumor_group="down"
  file$pattern=paste0("normal_",file$normal_group,"-tumor_",file$tumor_group)
  file$pattern.HC="other"
  file[file$pattern=="normal_down-tumor_down",]$pattern.HC="normal_down_tumor_down"
file[file$pattern=="normal_down-tumor_nosig"|file$pattern=="normal_nosig-tumor_down",]$pattern.HC="one_is_down"
file[file$pattern=="normal_down-tumor_up"|file$pattern=="normal_up-tumor_down",]$pattern.HC="onedown_oneup"
file[file$pattern=="normal_nosig-tumor_up"|file$pattern=="normal_up-tumor_nosig",]$pattern.HC="one_is_up"
file[file$pattern=="normal_up-tumor_up",]$pattern.HC="normal_up_tumor_up"
  file=file[,-9]
  file=file[,-9]
  file=file[,-9]
  names(file)=c("unitID",paste(group1[i],"BayesFactor",sep="."),
                paste0(unlist(strsplit(group1[i],"_"))[1],c(".bayes.beta0",".bayes.p.value")),
                paste0(unlist(strsplit(group1[i],"_"))[2],c(".bayes.beta0",".bayes.p.value")),
                paste0(unlist(strsplit(group1[i],"_"))[1],c(".FDR")),
                paste0(unlist(strsplit(group1[i],"_"))[2],c(".FDR")),
				paste0("pattern.HC.",group1[i]))
  rt=merge(rt,file,by="unitID",all=T)
}

rt2=tidyr::unite(rt,pattern.DC,paste0("pattern.DC.",group1[1:6]),sep=":",na.rm=TRUE,remove = FALSE)
rt2=tidyr::unite(rt2,pattern.CC,paste0("pattern.CC.",group1[7:10]),sep=":",na.rm=TRUE,remove = FALSE)
rt2=tidyr::unite(rt2,pattern.HC,paste0("pattern.HC.",group1[11:14]),sep=":",na.rm=TRUE,remove = FALSE)

rt2$DC.FDR.sig=rowSums(rt2[,grep(names(rt2),pattern = "FDR")[1:12]]<0.1,na.rm = T)
rt2$CC.FDR.sig=rowSums(rt2[,grep(names(rt2),pattern = "FDR")[13:20]]<0.1,na.rm = T)
rt2$HC.FDR.sig=rowSums(rt2[,grep(names(rt2),pattern = "FDR")[21:28]]<0.1,na.rm = T)
rt2$FDR.sig=rowSums(rt2[,grep(names(rt2),pattern = "FDR")][1:28]<0.1,na.rm = T)
rt2$pattern1.DC=rt2$pattern.DC
rt2$pattern1.CC=rt2$pattern.CC
rt2$pattern1.HC=rt2$pattern.HC
rt2=rt2[,-grep(names(rt2),pattern = "pattern.DC")[1]]
rt2=rt2[,-grep(names(rt2),pattern = "pattern.CC")[1]]
rt2=rt2[,-grep(names(rt2),pattern = "pattern.HC")[1]]

file=read.csv("20201112做汇总表/Loci.FDR0.1.statis.csv",head=T)
rt22=merge(rt2,file,by="unitID")
###
bed=tidyr::separate(rt,unitID,into=c("chrom","pos","ref","alt"))[,1:4]
bed1=data.frame(bed[,1:2],bed[,2:4])
dir.create("./20201112")
write.table(bed1,"./20201112/all.p.sig.for.annovar.txt",quote=F,row.names = F,sep = "\t")
###
anno=read.csv("./20201112/all.p.sig.anno.hg19_multianno.csv",header = T)
anno$unitID=paste(anno$Chr,anno$Start,anno$Ref,anno$Alt,sep=":")
anno=anno[,-3]
anno=data.frame(unitID=anno$unitID,anno[,1:8])
rt22=merge(anno,rt22,by="unitID")
write.csv(rt22,"./20201112做汇总表/all.FDR.sig.at.least.one.csv",quote=F,row.names = F)
###统计信息表
i=1
fn1=paste0(dir1,group1[i],".bayes_p.txt")
file=read.table(fn1,head=T,sep = "\t")
file$unitID=paste(file$chrom,file$position,sep=":")
file=data.frame(unitID=file$unitID,normal_bayes_beta0=file$normal_bayes_beta0,normal_bayes_pvalue=file$normal_bayes_pvalue,tumor_bayes_beta0=file$tumor_bayes_beta0,tumor_bayes_pvalue=file$tumor_bayes_pvalue)
file$FDR1=p.adjust(file$normal_bayes_pvalue,method = "BH")
file$FDR2=p.adjust(file$tumor_bayes_pvalue,method = "BH")
file=file[file$FDR1<0.1|file$FDR2<0.1,]
file$normal_group="nosig"
file[file$normal_bayes_beta0>0&file$FDR1<0.1,]$normal_group="up"
file[file$normal_bayes_beta0<0&file$FDR1<0.1,]$normal_group="down"
file$tumor_group="nosig"
file[file$tumor_bayes_beta0>0&file$FDR2<0.1,]$tumor_group="up"
file[file$tumor_bayes_beta0<0&file$FDR2<0.1,]$tumor_group="down"
file$pattern=paste0("normal_",file$normal_group,"-tumor_",file$tumor_group)
rt1=data.frame(table(file$pattern))
names(rt1)=c("Var1",group1[i])

for(i in 2:14){
fn1=paste0(dir1,group1[i],".bayes_p.txt")
file=read.table(fn1,head=T,sep = "\t")
file$unitID=paste(file$chrom,file$position,sep=":")
file=data.frame(unitID=file$unitID,normal_bayes_beta0=file$normal_bayes_beta0,normal_bayes_pvalue=file$normal_bayes_pvalue,tumor_bayes_beta0=file$tumor_bayes_beta0,tumor_bayes_pvalue=file$tumor_bayes_pvalue)
file$FDR1=p.adjust(file$normal_bayes_pvalue,method = "BH")
file$FDR2=p.adjust(file$tumor_bayes_pvalue,method = "BH")
file=file[file$FDR1<0.1|file$FDR2<0.1,]
file$normal_group="nosig"
file[file$normal_bayes_beta0>0&file$FDR1<0.1,]$normal_group="up"
file[file$normal_bayes_beta0<0&file$FDR1<0.1,]$normal_group="down"
file$tumor_group="nosig"
file[file$tumor_bayes_beta0>0&file$FDR2<0.1,]$tumor_group="up"
file[file$tumor_bayes_beta0<0&file$FDR2<0.1,]$tumor_group="down"
file$pattern=paste0("normal_",file$normal_group,"-tumor_",file$tumor_group)
test=data.frame(table(file$pattern))
names(test)=c("Var1",group1[i])
rt1=merge(rt1,test,by="Var1")
}
rt2=rt1
rt2[2,8:15]=rt2[2,8:15]+rt2[4,8:15]
rt2[4,8:15]=rep(NA,8)
rt2[3,8:15]=rt2[3,8:15]+rt2[6,8:15]
rt2[6,8:15]=rep(NA,8)
rt2[5,8:15]=rt2[5,8:15]+rt2[7,8:15]
rt2[7,8:15]=rep(NA,8)
write.csv(rt2,"./20201112做汇总表/FDR.0.1.sig.statis.csv",quote=F,row.names=F)

###分病种统计
###单发
i=1
fn1=paste0(dir1,group1[i],".bayes_p.txt")
file=read.table(fn1,head=T,sep = "\t")
file$unitID=paste(file$chrom,file$position,file$ref,file$var,sep=":")
file$id=paste(file$chrom,file$position,sep=":")
file=data.frame(unitID=file$unitID,normal_bayes_beta0=file$normal_bayes_beta0,normal_bayes_pvalue=file$normal_bayes_pvalue,tumor_bayes_beta0=file$tumor_bayes_beta0,tumor_bayes_pvalue=file$tumor_bayes_pvalue)
file$FDR1=p.adjust(file$normal_bayes_pvalue,method = "BH")
file$FDR2=p.adjust(file$tumor_bayes_pvalue,method = "BH")
file=file[file$FDR1<0.1|file$FDR2<0.1,]
file$normal_group="nosig"
file[file$normal_bayes_beta0>0&file$FDR1<0.1,]$normal_group="up"
file[file$normal_bayes_beta0<0&file$FDR1<0.1,]$normal_group="down"
file$tumor_group="nosig"
file[file$tumor_bayes_beta0>0&file$FDR2<0.1,]$tumor_group="up"
file[file$tumor_bayes_beta0<0&file$FDR2<0.1,]$tumor_group="down"
file$pattern=paste0("normal_",file$normal_group,"-tumor_",file$tumor_group)
names(file)=c("unitID",paste(group1[i],names(file)[2:7],sep="_"),names(file)[8:10])
normal_down_tumor_down=file[file$pattern=="normal_down-tumor_down",][,1:7]

normal_up_tumor_nosig=file[file$pattern=="normal_up-tumor_nosig",][,1:7]
normal_up_tumor_up=file[file$pattern=="normal_up-tumor_up",][,1:7]
normal_down_tumor_nosig=file[file$pattern=="normal_down-tumor_nosig",][,1:7]
normal_nosig_tumor_up=file[file$pattern=="normal_nosig-tumor_up",][,1:7]
normal_down_tumor_up=file[file$pattern=="normal_down-tumor_up",][,1:7]
normal_up_tumor_down=file[file$pattern=="normal_up-tumor_down",][,1:7]
normal_down_tumor_down=file[file$pattern=="normal_down-tumor_down",][,1:7]
normal_nosig_tumor_down=file[file$pattern=="normal_nosig-tumor_down",][,1:7]

for(i in 2:6){
fn1=paste0(dir1,group1[i],".bayes_p.txt")
file=read.table(fn1,head=T,sep = "\t")
file$unitID=paste(file$chrom,file$position,file$ref,file$var,sep=":")
file$id=paste(file$chrom,file$position,sep=":")
file=data.frame(unitID=file$unitID,normal_bayes_beta0=file$normal_bayes_beta0,normal_bayes_pvalue=file$normal_bayes_pvalue,tumor_bayes_beta0=file$tumor_bayes_beta0,tumor_bayes_pvalue=file$tumor_bayes_pvalue)
file$FDR1=p.adjust(file$normal_bayes_pvalue,method = "BH")
file$FDR2=p.adjust(file$tumor_bayes_pvalue,method = "BH")
file=file[file$FDR1<0.1|file$FDR2<0.1,]
file$normal_group="nosig"
file[file$normal_bayes_beta0>0&file$FDR1<0.1,]$normal_group="up"
file[file$normal_bayes_beta0<0&file$FDR1<0.1,]$normal_group="down"
file$tumor_group="nosig"
file[file$tumor_bayes_beta0>0&file$FDR2<0.1,]$tumor_group="up"
file[file$tumor_bayes_beta0<0&file$FDR2<0.1,]$tumor_group="down"
file$pattern=paste0("normal_",file$normal_group,"-tumor_",file$tumor_group)
names(file)=c("unitID",paste(group1[i],names(file)[2:7],sep="_"),names(file)[8:10])

normal_up_tumor_nosig=merge(normal_up_tumor_nosig,file[file$pattern=="normal_up-tumor_nosig",][,1:7],by="unitID",all=T)
  normal_up_tumor_up=merge(normal_up_tumor_up,file[file$pattern=="normal_up-tumor_up",][,1:7],by="unitID",all=T)
  normal_down_tumor_nosig=merge(normal_down_tumor_nosig,file[file$pattern=="normal_down-tumor_nosig",][,1:7],by="unitID",all=T)
  normal_nosig_tumor_up=merge(normal_nosig_tumor_up,file[file$pattern=="normal_nosig-tumor_up",][,1:7],by="unitID",all=T)
  normal_down_tumor_up=merge(normal_down_tumor_up,file[file$pattern=="normal_down-tumor_up",][,1:7],by="unitID",all=T)
  normal_up_tumor_down=merge(normal_up_tumor_down,file[file$pattern=="normal_up-tumor_down",][,1:7],by="unitID",all=T)
  normal_down_tumor_down=merge(normal_down_tumor_down,file[file$pattern=="normal_down-tumor_down",][,1:7],by="unitID",all=T)
  normal_nosig_tumor_down=merge(normal_nosig_tumor_down,file[file$pattern=="normal_nosig-tumor_down",][,1:7],by="unitID",all=T)
}
normal_up_tumor_nosig$pattern="normal_up_tumor_nosig"
normal_up_tumor_up$pattern="normal_up_tumor_up"
normal_down_tumor_nosig$pattern="normal_down_tumor_nosig"
normal_nosig_tumor_up$pattern="normal_nosig_tumor_up"
normal_down_tumor_up$pattern="normal_down_tumor_up"
normal_up_tumor_down$pattern="normal_up_tumor_down"
normal_down_tumor_down$pattern="normal_down_tumor_down"
normal_nosig_tumor_down$pattern="normal_nosig_tumor_down"

rt=rbind(normal_up_tumor_nosig,normal_up_tumor_up,normal_down_tumor_nosig,normal_nosig_tumor_up,normal_down_tumor_up,normal_up_tumor_down,normal_down_tumor_down,normal_nosig_tumor_down)
test=data.frame(table(rt$pattern))
write.csv(test,"./20201112做汇总表/DC.8pattern.union.statis.csv",quote=F,row.names = F)
###双发和健康
i=11
fn1=paste0(dir1,group1[i],".bayes_p.txt")
file=read.table(fn1,head=T,sep = "\t")
file$unitID=paste(file$chrom,file$position,file$ref,file$var,sep=":")
file$id=paste(file$chrom,file$position,sep=":")
file=data.frame(unitID=file$unitID,normal_bayes_beta0=file$normal_bayes_beta0,normal_bayes_pvalue=file$normal_bayes_pvalue,tumor_bayes_beta0=file$tumor_bayes_beta0,tumor_bayes_pvalue=file$tumor_bayes_pvalue)
file$FDR1=p.adjust(file$normal_bayes_pvalue,method = "BH")
file$FDR2=p.adjust(file$tumor_bayes_pvalue,method = "BH")
file=file[file$FDR1<0.1|file$FDR2<0.1,]
file$normal_group="nosig"
file[file$normal_bayes_beta0>0&file$FDR1<0.1,]$normal_group="up"
file[file$normal_bayes_beta0<0&file$FDR1<0.1,]$normal_group="down"
file$tumor_group="nosig"
file[file$tumor_bayes_beta0>0&file$FDR2<0.1,]$tumor_group="up"
file[file$tumor_bayes_beta0<0&file$FDR2<0.1,]$tumor_group="down"
file$pattern=paste0("normal_",file$normal_group,"-tumor_",file$tumor_group)
names(file)=c("unitID",paste(group1[i],names(file)[2:7],sep="_"),names(file)[8:10])
normal_down_tumor_down=file[file$pattern=="normal_down-tumor_down",][,1:7]
one_is_down=file[file$pattern=="normal_down-tumor_nosig"|file$pattern=="normal_nosig-tumor_down",][,1:7]
onedown_oneup=file[file$pattern=="normal_down-tumor_up"|file$pattern=="normal_up-tumor_down",][,1:7]
one_is_up=file[file$pattern=="normal_nosig-tumor_up"|file$pattern=="normal_up-tumor_nosig",][,1:7]
normal_up_tumor_up=file[file$pattern=="normal_up-tumor_up",][,1:7]


for(i in 12:14){
  fn1=paste0(dir1,group1[i],".bayes_p.txt")
file=read.table(fn1,head=T,sep = "\t")
file$unitID=paste(file$chrom,file$position,file$ref,file$var,sep=":")
file$id=paste(file$chrom,file$position,sep=":")
file=data.frame(unitID=file$unitID,normal_bayes_beta0=file$normal_bayes_beta0,normal_bayes_pvalue=file$normal_bayes_pvalue,tumor_bayes_beta0=file$tumor_bayes_beta0,tumor_bayes_pvalue=file$tumor_bayes_pvalue)
file$FDR1=p.adjust(file$normal_bayes_pvalue,method = "BH")
file$FDR2=p.adjust(file$tumor_bayes_pvalue,method = "BH")
file=file[file$FDR1<0.1|file$FDR2<0.1,]
file$normal_group="nosig"
file[file$normal_bayes_beta0>0&file$FDR1<0.1,]$normal_group="up"
file[file$normal_bayes_beta0<0&file$FDR1<0.1,]$normal_group="down"
file$tumor_group="nosig"
file[file$tumor_bayes_beta0>0&file$FDR2<0.1,]$tumor_group="up"
file[file$tumor_bayes_beta0<0&file$FDR2<0.1,]$tumor_group="down"
file$pattern=paste0("normal_",file$normal_group,"-tumor_",file$tumor_group)
names(file)=c("unitID",paste(group1[i],names(file)[2:7],sep="_"),names(file)[8:10])
  normal_down_tumor_down=merge(normal_down_tumor_down,file[file$pattern=="normal_down-tumor_down",][,1:7],by="unitID",all=T)
  one_is_down=merge(one_is_down,file[file$pattern=="normal_down-tumor_nosig"|file$pattern=="normal_nosig-tumor_down",][,1:7],by="unitID",all=T)
  onedown_oneup=merge(onedown_oneup,file[file$pattern=="normal_down-tumor_up"|file$pattern=="normal_up-tumor_down",][,1:7],by="unitID",all=T)
  one_is_up=merge(one_is_up,file[file$pattern=="normal_nosig-tumor_up"|file$pattern=="normal_up-tumor_nosig",][,1:7],by="unitID",all=T)
  normal_up_tumor_up=merge(normal_up_tumor_up,file[file$pattern=="normal_up-tumor_up",][,1:7],by="unitID",all=T)
}
normal_down_tumor_down$pattern2="normal_down_tumor_down"
one_is_down$pattern2="one_is_down"
onedown_oneup$pattern2="onedown_oneup"
one_is_up$pattern2="one_is_up"
normal_up_tumor_up$pattern2="normal_up_tumor_up"
rt=rbind(normal_down_tumor_down,one_is_down,onedown_oneup,one_is_up,normal_up_tumor_up)
test=data.frame(table(rt$pattern2))
write.csv(test,"./20201112做汇总表/HC.5pattern.union.statis.csv",quote=F,row.names = F)
